package com.andy.base.mapper;

import com.andy.base.util.UrlSplitUtil;

import java.util.HashMap;
import java.util.Map;

public class Host implements Mapper {

    Map<String,Context> contextMap = new HashMap<>();

    public Host(Map<String, Context> contextMap) {
        this.contextMap = contextMap;
    }

    /**
     * map到需要的对象,然后放入mappingData
     *
     * @param url
     * @param mappingData
     * @return
     * @throws Exception
     */
    @Override
    public Mapper map(String url, MappingData mappingData) throws Exception {
        String contextKey = UrlSplitUtil.getContext(url);
        Context context = contextMap.get(contextKey);

        mappingData.setContext(context);
        mappingData.setContextKey(contextKey);

        return context;
    }

    /**
     * 是否需要继续map
     *
     * @return
     */
    @Override
    public boolean needMapping() {
        return true;
    }
}
